<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hadd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hedit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hdel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageparams.page"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageparams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hclose"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      isEdit: false,
      showDialog: false,
      list: [],
      total: 0,
      pageparams: {
        page: 1,
        pagesize: 5
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [],
        description: []
      }
    }
  },
  computed: {
    max() {
      return Math.ceil(this.total / this.pageparams.pagesize)
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // @close 弹框从true变成false时触发的事件
    hclose() {
      // resetFields() 只能让表单回到最初的状态，点击编辑后表单初始就是有数据的所以用了
      //  resetFields() 方法不能让表单清空，但是可以清空校验规则
      this.$refs.roleForm.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 确定按钮，需要兜底校验，假设校验已做
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doadd()
    },
    // 添加请求
    async doadd() {
      await addRole(this.roleForm)
      // console.log(res)
      this.pageparams.page = this.max
      this.loadRoles()
      this.showDialog = false
    },
    // 编辑请求
    async doEdit() {
      console.log(111111)
      const res = await updateRole(this.roleForm)
      console.log(res)
      this.loadRoles()
      this.showDialog = false
    },
    // 编辑弹框显示
    hedit(row) {
      this.showDialog = true
      this.isEdit = true
      this.roleForm = { ...row }
    },
    // 添加弹框显示
    hadd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 删除
    async hdel(id) {
      await deleteRole(id)
      // console.log(res)
      // 如果当前页最后一条，让页数 --
      if (this.list.length === 1) {
        this.pageparams.page--
      }
      // 如果页数 <= 0 就让页数等于 1
      if (this.pageparams.page <= 0) {
        this.pageparams.page = 1
      }
      this.loadRoles()
    },
    // 分页组件
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageparams.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageparams.page = val
      this.loadRoles()
    },
    // 获取角色列表
    async loadRoles() {
      const res = await getRoles(this.pageparams)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
